<?php

namespace cm\app\sms;

require_once __DIR__ . '/aliyun/TopSdk.php';

class SmsAliyun
{
    private $appkey;
    private $secretKey;
    private $sign;

    function __construct($config)
    {
        $this->appkey = $config['appkey'];
        $this->secretKey = $config['secretKey'];
        $this->sign = $config['sign'];
    }

    private function exe($req)
    {
        $topClient = new \TopClient;
        $topClient->appkey = $this->appkey;
        $topClient->secretKey = $this->secretKey;
        $resp = $topClient->execute($req);
        return $resp;
    }

    public function send($mobiles, $tempID, $param,$number)
    {
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend($number);
        $req->setSmsType("normal");
        $req->setSmsFreeSignName($this->sign);
        $req->setSmsParam($param);//
        $req->setRecNum($mobiles);
        $req->setSmsTemplateCode($tempID);//

        $resp = $this->exe($req);
        return $resp;
    }

    /**
     * 发送注册验证码
     * @param $mobile
     * @param $code
     * @return mixed|\ResultSet|\SimpleXMLElement
     */
    public function sendRegCode($mobile, $code)
    {
        $tempID = "SMS_127780006";
        $param = "{\"code\":\"{$code}\"}";
        return $this->send($mobile, $tempID, $param,PAGE_NUMBER);
    }

//    public function sendResetPwdCode($mobile, $code)
//    {
//        $tempID = "SMS_46435005";
//        $param = "{\"code\":\"{$code}\"}";
//        return $this->send($mobile, $tempID, $param,PAGE_NUMBER);
//    }

    public function queryAll()
    {
        $req = new \TmcMessagesConsumeRequest;
        $req->setQuantity('200');

        $resp = $this->exe($req);
        return $resp;
    }

    /**
     * @param $telnumber
     * @param $date 20151215
     */
    public function queryOne($telphone, $date, $pageIndex = 1, $pageSize = 50)
    {
        $req = new \AlibabaAliqinFcSmsNumQueryRequest;
        //$req->setBizId("1234^1234");
        $req->setRecNum($telphone);
        $req->setQueryDate($date);
        $req->setCurrentPage("{$pageIndex}");
        $req->setPageSize("{$pageSize}");

        $resp = $this->exe($req);

        return $resp;
    }

    public function queryStatus($smsNumber, $telphone, $date)
    {
        //status 发送状态 1：等待回执，2：发送失败，3：发送成功
        $result = array("msg" => "", "status" => "1");//, "data" => array()

        $resp = $this->queryOne($telphone, $date);
        $jsonResp = json_decode(json_encode($resp), true);

        if (isset($jsonResp['sub_msg'])) {
            $result["msg"] = $jsonResp['sub_msg'];
            return $result;
        }

        $list = isset($jsonResp["values"]["fc_partner_sms_detail_dto"]) ? $jsonResp["values"]["fc_partner_sms_detail_dto"] : array();

        if (isset($list['extend'])) {
            if ($list['extend'] == $smsNumber) {
                //$result["data"] = $row;
                $result["status"] = $list["sms_status"];
                return $result;
            }
        }

        if (is_array($list))
            foreach ($list as $row) {
                if ($row["extend"] == $smsNumber) {
                    //$result["data"] = $row;
                    $result["status"] = $row["sms_status"];
                    return $result;
                }
            }

        $total_page = isset($jsonResp["total_page"]) ? $jsonResp["total_page"] : 0;

        if ($total_page > 1) {
            for ($i = 2; $i <= $total_page; $i++) {

                $resp = $this->queryOne($telphone, $date, $i);
                $jsonResp = json_decode(json_encode($resp), true);
                if (isset($jsonResp["sub_msg"])) {
                    $result["msg"] = $jsonResp["sub_msg"];
                    return $result;
                }


                $list = isset($jsonResp["values"]["fc_partner_sms_detail_dto"]) ? $jsonResp["values"]["fc_partner_sms_detail_dto"] : array();

                if (is_array($list))
                    foreach ($list as $row) {
                        if ($row["extend"] == $smsNumber) {
                            //$result["data"] = $row;
                            $result["status"] = $row["sms_status"];
                            return $result;
                        }
                    }
            }
        }

        $result["msg"] = "";

        return $result;
    }
}